﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Game.Foundation;
using Bruce.Net;
using Server.Game.Generic;

namespace Game.Server.ChineseChess
{
    class GameController
    {
        private static List<GameAreaServer> m_areaList;
        public static int SeatCountPerGroup { get { return 2; } }
        public static string GameName { get; private set; }
        public static string IPAddress { get; private set; }
        public static int Port { get; private set; }
        public static string RelativePath { get; private set; }
        public static string DownLoadPath { get; private set; }

        static GameController()
        {
            XDocument document = XDocument.Load("config/gameconfig.xml");
            GameName = document.Root.Attribute("name").Value;
            IPAddress = document.Root.Attribute("ip").Value;
            Port = int.Parse(document.Root.Attribute("port").Value);
            RelativePath = document.Root.Attribute("relativepath").Value;
            DownLoadPath = document.Root.Attribute("downloadpath").Value;

            m_areaList = new List<GameAreaServer>();
            var areas = (from e in document.Root.Elements("area")
                         select e);

            int areaId = 1;
            foreach (XElement item in areas)
            {
                GameAreaServer area = new GameAreaServer
                {
                    ID = areaId++,
                    Name = item.Attribute("name").Value,
                };


                var rooms = from r in item.Elements("room")
                            select r;

                int roomId = 1;
                foreach (var room in rooms)
                {
                    GameRoomServer gr = new GameRoomServer(area)
                    {
                        ID = roomId++,
                        Name = room.Attribute("name").Value,
                    };
                    for (int i = 1; i <= int.Parse(room.Attribute("desk").Value); i++)
                    {
                        ChineseChessGameGroup group = new ChineseChessGameGroup(gr, i);
                        gr.RegisterDesk(group);
                    }

                    area.RegisterRoom(gr);
                }

                m_areaList.Add(area);
            }
        }

        public static GameAreaServer GetArea(int areaId)
        {
            return m_areaList.SingleOrDefault(a => a.ID == areaId);
        }

        public static GameRoomServer GetRoom(int areaId, int roomId)
        {
            GameAreaServer area = GetArea(areaId);
            if (area == null) return null;

            return area.GetRoom(roomId);
        }

        public static AbsGameGroup GetDesk(int areaId, int roomId, int deskId)
        {
            GameRoomServer room = GetRoom(areaId, roomId);
            if (room == null) return null;

            return room.GetDesk(deskId);
        }

        public static Packet GeneratePacket()
        {
            Packet p = new Packet((int)PacketType.注册游戏处理程序);
            p.WriteString(GameController.GameName);
            p.WriteString(GameController.IPAddress);
            p.WriteInt32(GameController.Port);
            p.WriteInt32(GameController.SeatCountPerGroup);
            p.WriteString(GameController.RelativePath);
            p.WriteString(GameController.DownLoadPath);
            p.WriteInt32(GameController.m_areaList.Count);
            foreach (GameAreaServer area in m_areaList)
            {
                p.WriteBytes(area.GetBytes());
            }

            return p;
        }
    }
}
